package com.ywb.edugui.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.util.StyleUtil;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.WriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.ywb.edugui.dao.SubjectDao;
import com.ywb.edugui.dao.facotry.DaoFactory;
import com.ywb.edugui.entity.Subject;
import com.ywb.edugui.entity.Teacher;
import com.ywb.edugui.entity.vo.SubjectExcel;
import com.ywb.edugui.entity.vo.TeacherExcel;
import org.apache.poi.ss.usermodel.*;

import java.awt.Color;
import java.io.*;
import java.util.List;

public class ExcelUtils {
    /**
     * 先使用EasyExcel读出单元格的数据后,再判断是否存在数据库中,最后添加
     * @param file
     */
    public static void addSubjectByExcel(File file){
        try{
            FileInputStream fis = new FileInputStream(file);
            EasyExcel.read(fis, SubjectExcel.class, new SubjectExcelListener()).sheet().doRead();
            System.out.println("添加成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
    public static void exportTeacherExcel(String filepath, List<TeacherExcel> list){
        try{
            EasyExcel.write(filepath, TeacherExcel.class).sheet("sheet 1").registerWriteHandler(new TeacherExcelListener()).doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * teacher列表导出单元格处理器
     */
    static class TeacherExcelListener implements CellWriteHandler {


        @Override
        public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {

        }

        @Override
        public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {

        }

        @Override
        public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
            /**
             * 设置边框、字体、背景色、居中
             */
            WriteCellStyle writeCellStyle = new WriteCellStyle();
            // 设置单元格上下左右边框为细边框
            writeCellStyle.setBorderBottom(BorderStyle.MEDIUM);
            writeCellStyle.setBorderLeft(BorderStyle.MEDIUM);
            writeCellStyle.setBorderRight(BorderStyle.MEDIUM);
            writeCellStyle.setBorderTop(BorderStyle.MEDIUM);

            WriteFont writeFont = new WriteFont();
            writeFont.setFontName("思源黑体");
            writeFont.setFontHeightInPoints((short) 12);
            writeFont.setBold(false);
            writeCellStyle.setWriteFont(writeFont);

            writeCellStyle.setFillForegroundColor(IndexedColors.WHITE1.getIndex());
            writeCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            writeCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

            Workbook workbook = writeSheetHolder.getSheet().getWorkbook();
            cell.setCellStyle(StyleUtil.buildHeadCellStyle(workbook, writeCellStyle));

        }
    }
    static class SubjectExcelListener extends AnalysisEventListener<SubjectExcel> {
        private SubjectDao subjectDao = DaoFactory.getSubjectDao();
        //每次都会读取一行excel数据进来
        @Override
        public void invoke(SubjectExcel subjectExcel, AnalysisContext analysisContext) {
            if(subjectExcel == null)
                throw new RuntimeException(getClass().getSimpleName() + ": 文件数据为空");
            Subject parent = subjectDao.getByTitle(subjectExcel.getFirstSubjectName());
//        如果不存在parent则新增一个一级科目
            if(parent == null){
                parent = new Subject();
                parent.setTitle(subjectExcel.getFirstSubjectName());
                parent.setParent_id("0");
                subjectDao.addBatchParams(parent);
            }
            Subject child = subjectDao.getByTitle(subjectExcel.getSecondSubjectName());
            if(child == null){
                child = new Subject();
                child.setTitle(subjectExcel.getSecondSubjectName());
                child.setParent_id(parent.getId());
                subjectDao.addBatchParams(child);
            }
            System.out.println(parent + ":" + child);
        }


        @Override
        public void doAfterAllAnalysed(AnalysisContext analysisContext) {
            subjectDao.addBatch();
        }
    }

}
